Skip to content

Conversation

@tmds
Copy link
Member

@tmds tmds commented Jan 7, 2026

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 7, 2026
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 7, 2026
default:
return true; // in all other situations it should be OK
}
return Interop.Sys.FileSystemSupportsLocking(this);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior here is changing. Previously, if we didn't recognize the type as one of the values in UnixFileSystemTypes then no locking was performed. Now, we'll do locking unless the type is nfs/smb/cifs.

I assume we're ok to do this as return true also happens in earlier checks.

This does mean that we rely on the string names in PAL to be correct for identifying nfs/smb/cifs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does mean that we rely on the string names in PAL to be correct for identifying nfs/smb/cifs.

Isn't it the same as main branch? f_type on linux (comparison with magic integers) and f_fstypename / f_basetype on other Unix (string comparison with strcmp)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose we have the names wrong.

On the main branch, MapFileSystemNameToEnum returns zero, then TryGetFileSystemType return false and then CanLockTheFile will return false.

With the new implementation, FileSystemNameSupportsLocking returns 1 and then FileSystemSupportsLocking will return true.

Copy link
Member

@jkotas jkotas Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above says "// LOCK_SH is not OK when writing to NFS, CIFS or SMB". Why is it not OK? What's the failure more?

I am trying to better understand the downside of enabling locking by default for all file systems except the few we explicitly check for.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locking is enabled by default to match Windows behavior.
(For example: you get an exception when the SDK tries to write the same file simultaneously.)

The code is filtering for a combination that doesn't work well.
The combination seems to be: LOCK_SH + FileAccess.Write + nfs/smb/cifs.
And the referenced issues mention silently discarded writes and throwing on dispose (#44546, #53182).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like that this is a workaround for a buggy implementation of locks in some file systems. I think it is fine to assume that unknown file systems have a correct lock implementation.

cc @adamsitnik

Copy link
Member

@am11 am11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks! :)

@tmds
Copy link
Member Author

tmds commented Jan 9, 2026

Thanks for the suggestions/review, @am11!

@jkotas jkotas requested a review from adamsitnik January 11, 2026 15:50
default:
return true; // in all other situations it should be OK
}
return Interop.Sys.FileSystemSupportsLocking(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Interop.Sys.FileSystemSupportsLocking(this);
// LOCK_SH does not work well for write access in some file systems. For example, writes are dropped silently.
// We try to detect the problematic cases and disable use of LOCK_SH for write access for them.
// See https://github.com/dotnet/runtime/issues/44546 and https://github.com/dotnet/runtime/issues/53182
return Interop.Sys.FileSystemSupportsLocking(this);

default:
return true; // in all other situations it should be OK
}
return Interop.Sys.FileSystemSupportsLocking(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this method have a better name to make it clear that this is just about the SH_LOCK + write combination?

Alternatively, this method can take the lock type and bool to indicate write access to make the contract easier to explain.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, this method can take the lock type and bool to indicate write access to make the contract easier to explain.

I've implemented this option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants